package com.tskj.mh.badminton.controller;

import com.alibaba.fastjson.JSONObject;
import com.tskj.mh.badminton.aop.annotation.SystemControllerLog;
import com.tskj.mh.badminton.service.StudentService;
import com.tskj.mh.badminton.service.BadmintonService;
import com.tskj.mh.badminton.service.LoginService;
import com.tskj.mh.badminton.utility.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author LeonSu
 */
@Controller
@RequestMapping("/badminton")
public class BadmintonController {
    @Autowired
    private BadmintonService badmintonService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private StudentService studentService;

    /**
     * 获取理发店列表
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public JSONObject getList() {
        JSONObject ret = new JSONObject();
        ret.put("code", 0);
        ret.put("data", badmintonService.getRoomList());
        return ret;
    }

    /**
     * 获取理发时间表
     * @param BadmintonId
     * @param date
     * @return
     */
    @GetMapping("/timeList")
    @ResponseBody
    public JSONObject getTimeList(@RequestParam String BadmintonId, @RequestParam String date) {
        JSONObject ret = new JSONObject();
        ret.put("code", 0);
        ret.put("data", badmintonService.getTimeList(BadmintonId, date));
        return ret;
    }

    /**
     * 保存理发订单
     * @param data
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    @SystemControllerLog(MODULENAME = "羽毛球")
    public JSONObject saveOrder(@RequestParam String token, @RequestBody JSONObject data, HttpServletResponse response) {
        String adUserName = loginService.getAdUserName(token);
        JSONObject student = studentService.getStudentInfo(adUserName);
        try {
            return badmintonService.saveOrderToQuene(token, student, data);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject ret = new JSONObject();
            ret.put("code", 1);
            ret.put("errMsg", e.getMessage());
            System.out.println(ret.toJSONString());
            return ret;
        }
    }

    @GetMapping("/checkStopTime")
    @ResponseBody
    public JSONObject checkStopTime(@RequestParam String token) {
        String adUserName = loginService.getAdUserName(token);
        return badmintonService.checkStopTime(adUserName, Tools.today());
    }
}
